package com.cookman.gameobjects;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.cookman.gameobjects.decks.PlayerDeckActor;
import com.cookman.serviceLocators.ServiceLocator;
import com.cookman.utils.AssetUtils;
import com.cookman.world.entities.Player;

/**
 * Created by Cookman on 04.09.14.
 */
public class PlayerActor extends AbstractGameObject {

    private Player player;
    private PlayerDeckActor parentDeck;
    protected float scale = 0.4f;
    private Sprite playerIcon;
    private Boolean isSelected = false;

    public PlayerActor(Player player, PlayerDeckActor parent) {
        this.player = player;
        this.parentDeck = parent;
        playerIcon= AssetUtils.getPlayerSprite(player);
        setBounds(this.getX(), this.getY(), scale, scale * (playerIcon.getHeight() / playerIcon.getWidth()));
        addListener(mouseActorListener);
        setTouchable(Touchable.enabled);
    }

    public void setIsSelected(Boolean isSelected) {
        this.isSelected = isSelected;
    }

    public Boolean getIsSelected() {
        return isSelected;
    }


    @Override
    public void draw(Batch batch, float parentAlpha) {
        if (isSelected) {
            batch.draw(playerIcon, getX(), getY(), scale * 1.05f, scale * (playerIcon.getHeight() / playerIcon.getWidth()) * 1.05f);
        } else {
            batch.draw(playerIcon, getX(), getY(), scale, scale * (playerIcon.getHeight() / playerIcon.getWidth()));
        }
    }

    private static InputListener mouseActorListener = new InputListener() {
        @Override
        public boolean touchDown(InputEvent event, float x, float y,
                                 int pointer, int button) {
            return true;
        }

        @Override
        public void touchUp(InputEvent event, float x, float y, int pointer,
                            int button) {
            if(event.getTarget() instanceof PlayerActor) {
                PlayerActor actor = (PlayerActor) event.getTarget();
                System.out.println("Player card touched " + actor.player.getRole().toString());
                actor.parentDeck.resetSelection();
                actor.setIsSelected(true);
                ServiceLocator.getUIController().selectPlayer(actor.player);
            }
        }

        @Override
        public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
            super.enter(event, x, y, pointer, fromActor);
        }
    };

    @Override
    public void renderGUI(SpriteBatch batch) {

    }

    @Override
    public void renderShape(ShapeRenderer renderer) {

    }
}
